package com.selfdriving.wl.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.adapter.SearchAdapter;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.FindScenic;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.GB2Alpha;
import com.selfdriving.view.AlphabetListView;
import com.selfdriving.view.AlphabetListView.AlphabetPositionListener;

public class SearchFeature
{
	/** 当前View **/
	View view;

	AlphabetListView listView;
	BaseApplication application;
	Context context;
	private List<Entity> list = new ArrayList<Entity>();
	private SearchAdapter adapter;
	GB2Alpha obj1 = new GB2Alpha();

	public SearchFeature(BaseApplication application, Context context)
	{
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.search, null);
		this.application = application;
		this.context = context;
		listView = (AlphabetListView) view.findViewById(R.id.feature_list);
		getListDatas();
	}

	public void initViews()
	{

		adapter = new SearchAdapter(application, context, list);

		AlphabetPositionListener positionListener = new AlphabetPositionListener()
		{
			@Override
			public int getPosition(String letter)
			{
				for (int i = 0, count = list.size(); i < count; i++)
				{
					FindScenic article = (FindScenic) list.get(i);
					Character firstLetter = obj1.String2Alpha(article.getKey());
					if (firstLetter.toString().equals(letter))
					{
						return i;
					}
				}
				return UNKNOW;
			}
		};

		listView.setAdapter(adapter, positionListener);
	}

	public void getListDatas()
	{
		list.clear();
		listView.removeAllViews();
		AjaxParams params = new AjaxParams();
		params.put("f", "16");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString().trim();
				try
				{
					JSONArray array = new JSONArray(json);
					FindScenic findScenic;
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String id = object.getString("id");
						String name = object.getString("name");
						String key = object.getString("key");
						String province = object.getString("province");
						String policy = object.getString("policy");
						String price = object.getString("price");
						String clicks = object.getString("clicks");
						String lat = object.getString("lat");
						String lng = object.getString("lng");
						String img0 = null;
						String img1 = null;
						String img2 = null;
						if (object.has("img0"))
						{
							img0 = object.getString("img0");
						}
						if (object.has("img1"))
						{
							img1 = object.getString("img1");
						}
						if (object.has("img2"))
						{
							img2 = object.getString("img2");
						}
						findScenic = new FindScenic(id, name, key, price,
								province, policy, clicks, lat, img0, img1,
								img2, lng);
						list.add(findScenic);
					}
					initViews();
				} catch (JSONException e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	public View getView()
	{
		return view;
	}

}
